//
//  AOE_XML_Parser.m
//  AOEProject
//
//  Created by techplus on 9/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#define TAG_CATALOG @"Catalogue"
#define TAG_CATALOG_NAME_ATR @"Name"

#define TAG_CATALOG_ELEMENT @"CatalogueElement"
#define TAG_CATALOG_ELEMENT_NAME_ATR @"Name"

#define TAG_MATCH @"M"
#define TAG_MATCH_NAME_ATR @"N"
#define TAG_MATCH_DATE_ATR @"D"
#define TAG_MATCH_TYPE_ATR @"L"

#define TAG_GAME @"G"
#define TAG_GAME_T_ATR @"T"
#define TAG_GAME_URL_ATR @"U"


#import "AOE_XML_Parser.h"
#import "Catalogue.h"
#import "CatalogueElement.h"
#import "Match.h"
#import "Game.h"


@implementation AOE_XML_Parser
@synthesize hotnewParser;
@synthesize dataParser;
-(id)initSingleton
{
    self = [super init];
    if(self)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [paths objectAtIndex:0];
        
        //SUMARY
        NSString *xmlFilePath = [NSString stringWithFormat:@"%@%@",documentDirectory,@"data.xml"];
        
        NSString *xmlFilePath2 = [NSString stringWithFormat:@"%@%@",documentDirectory,@"data.hotnew.xml"];
        
        hotnewParser = [[GDataXMLDocument alloc]initWithXMLString:[NSString stringWithContentsOfFile:xmlFilePath2 encoding:NSUTF8StringEncoding error:nil] options:nil error:nil];
        
        dataParser = [[GDataXMLDocument alloc]initWithXMLString:[NSString stringWithContentsOfFile:xmlFilePath encoding:NSUTF8StringEncoding error:nil] options:nil error:nil];
        
        
        for(GDataXMLNode *n in [[hotnewParser rootElement]children])
        {
            GDataXMLElement *catalog = [GDataXMLNode elementWithName:@"Catalogue"];
            [catalog addChild:n];
            [[dataParser rootElement]addChild:catalog];
            //[catalog release];
        }
        
        //NSLog(@"%d",[[[dataParser rootElement] elementsForName:TAG_CATALOG]count]);
        //[hotnewParser release];
        //NSLog(@"%d", [[dataParser rootElement]childCount]);

    }
    return self;
}

+(AOE_XML_Parser *) getInstance
{
    static AOE_XML_Parser *_default = nil;
    if(_default !=nil)
    {
        return _default;
    }
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        _default = [[AOE_XML_Parser alloc]initSingleton];
    });
    return _default;
}

-(NSMutableArray *)getCatalogElementList:(NSInteger *)catalogIndex{
    
    NSMutableArray *catalogElements = [[[NSMutableArray alloc]init ]autorelease];
    
    NSArray *getData = [[dataParser rootElement] elementsForName:TAG_CATALOG];
    
    GDataXMLElement *e = [getData objectAtIndex:(int)catalogIndex];
    NSArray *temp = [e elementsForName:TAG_CATALOG_ELEMENT];
    
    int i = 0;
    for(GDataXMLElement *catalogElement in temp)
    {
        CatalogueElement *ce = [[CatalogueElement alloc]initWithCatalogIndexAndCEIndex:catalogIndex :(NSInteger *)i];
        ce.atrName = [[catalogElement attributeForName:TAG_CATALOG_ELEMENT_NAME_ATR]stringValue];
        //NSLog(ce.atrName);
        [catalogElements addObject:ce];
        [ce release];
        i++;
    }
    return catalogElements;
    
};

-(NSMutableArray *)getMatchElementList:(NSInteger *)catalogIndex:(NSInteger *)ceIndex{
    
    NSMutableArray *matchs = [[NSMutableArray alloc]init];
    
    NSArray *temp = [[[[[[dataParser rootElement]elementsForName:TAG_CATALOG]objectAtIndex:(int)catalogIndex]elementsForName:TAG_CATALOG_ELEMENT]objectAtIndex:(int)ceIndex]elementsForName:TAG_MATCH];
    
    int i = 0;
    for(GDataXMLElement *match in temp)
    {
        Match *m = [[Match alloc]initWithCatalogIndex:catalogIndex ceIndex:ceIndex matchIndex:(NSInteger *)i];
        m.atrTitle = [[match attributeForName:TAG_MATCH_NAME_ATR]stringValue];
        m.atrDate = [[match attributeForName:TAG_MATCH_DATE_ATR]stringValue];
        m.atrType = [[match  attributeForName:TAG_MATCH_TYPE_ATR]stringValue];
        [matchs addObject:m];
        [m release];
        i++;
    }
    [temp release];
    return matchs;
    
    
};


-(NSMutableArray *)getGameList:(NSInteger *)catalogIndex:(NSInteger *)ceIndex:(NSInteger *)mIndex{
    NSMutableArray *games = [[NSMutableArray alloc]init];
    NSArray *temp = [[[[[[[[dataParser rootElement]elementsForName:TAG_CATALOG]objectAtIndex:(int)catalogIndex]elementsForName:TAG_CATALOG_ELEMENT]objectAtIndex:(int)ceIndex]elementsForName:TAG_MATCH]objectAtIndex:(int)mIndex]elementsForName:TAG_GAME];
    
    int i =0;
    for(GDataXMLElement *game in temp)
    {
        Game *g = [[Game alloc]initWithCatalogIndex:catalogIndex :ceIndex :mIndex :(NSInteger *)i];
        g.atrName = [[game attributeForName:TAG_GAME_T_ATR]stringValue];
        g.gUrl = [[game attributeForName:TAG_GAME_URL_ATR]stringValue];
        [games addObject:g];
        [g release];
    }
    [temp release];
    return games;
};


-(void)dealloc{
    [dataParser release];
    [hotnewParser release];
    [super dealloc];
}
@end
